﻿using System.Data;
using ContractManagementSystem.DataAccessLayer;
using ContractManagementSystem.EntityObjects;

namespace ContractManagementSystem.BusinessLogicLayer
{
    public static class ContractorProfileLogic
    {
        #region"Get contractors"

        //get all data client
        public static DataTable SelectData()
        {
            return ContractorDao.SelectAllData();
        }
        //get contractor by it
        public static DataTable GetContractorByContractorId(string contractorId)
        {
            return ContractorDao.GetContractorByContractorId(contractorId);
        }
        //Get contractor by user name
        public static DataTable GetContractorByContractorUserName(string userName)
        {
            return ContractorDao.GetContractorByContractorUserName(userName);
        }
        //get contractor by client
        public static DataTable GetContractorByClient(string userName)
        {
            return ContractorDao.GetContractorByClient(userName);
        }
        //get contractor by user name of consultant
        public static DataTable GetContractorByConsultant(string userName)
        {
            return ContractorDao.GetContractorByConsultant(userName);
        }

        #endregion

        #region"Search"

        //search by role admin
        public static DataTable SearchContractor(string fullName, string email)
        {
            var result = ContractorDao.SearchContractor(fullName, email);
            return result;
        }
        //search by role client
        public static DataTable SearchContractorByClient(string user,string fullName, string email)
        {
            var result = ContractorDao.SearchContractorByClient(user, fullName, email);
            return result;
        }
        //search by role consultant
        public static DataTable SearchContractorByConsultant(string user, string fullName, string email)
        {
            var result = ContractorDao.SearchContractorByConsultant(user, fullName, email);
            return result;
        }

        #endregion

        #region"Insert/delete/update"
        //insert contractor
        public static string InsertContractor(ContractorEntity contractor)
        {
            if (!CheckBeforeSave(contractor))
            {
                return null;
            }
            if (CheckExistEmailValue(contractor.Email))
            {
                return "Existing email!";
            }
            string insert = ContractorDao.Insert(contractor);
            return insert;
        }
        //update contractor
        public static string UpdateContractor(ContractorEntity contractor)
        {
            if (!CheckBeforeSave(contractor))
            {
                return null;
            }
            string update = ContractorDao.Update(contractor);
            return update;
        }
        //delete contractor
        public static string DeleteContractor(ContractorEntity contractor)
        {
            string delete = ContractorDao.Delete(contractor);
            return delete;
        }
        #endregion

        //check exists email
        public static bool CheckExistEmailValue(string value)
        {
            bool result = ContractorDao.CheckExistEmailValue(value);
            return result;
        }

        //check emtry before save
        public static bool CheckBeforeSave(ContractorEntity contractor)
        {
            if(string.IsNullOrEmpty(contractor.FullName))
            {
                return false;
            }
            if(string.IsNullOrEmpty(contractor.Email))
            {
                return false;
            }
            if(string.IsNullOrEmpty(contractor.PhoneNumber1))
            {
                return false;
            }
            if(string.IsNullOrEmpty(contractor.Branch))
            {
                return false;
            }
            if(string.IsNullOrEmpty(contractor.SortCode))
            {
                return false;
            }
            return true;
        }

    }
}
